package org.example.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.example.entity.Clubs;
import org.example.entity.Players;
import org.example.service.ClubsService;
import org.example.service.PlayersService;
import org.example.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@Controller
@RequestMapping("/players")
public class PlayersController {
    @Autowired
    private PlayersService playersService;
    @Autowired
    private ClubsService clubsService;
    @Autowired
    private RedisUtils redisUtils;

    @GetMapping("/add")
    public String add(){
        return "play/add";
    }



    @GetMapping("/clubs")
    @ResponseBody
    public String clubsList() throws JsonProcessingException {
        String clubString = redisUtils.get("clubs");
        if(clubString == null){
            List<Clubs> clubsList = clubsService.getList();
            clubString =new ObjectMapper().writeValueAsString(clubsList);
            redisUtils.set("clubs",clubString);
            return clubString;
        }else {
            return clubString;
        }
    }

    @RequestMapping("/list")
    public String getList(String pname, Integer cid , Model model,
                          @RequestParam(defaultValue = "1") Integer pageNo){
        Integer pageSize = 3;
        PageHelper.startPage(pageNo,pageSize);
        List<Players> list = playersService.getList(pname,cid,pageNo);
        model.addAttribute("playersList",list);
        model.addAttribute("pname",pname);
        model.addAttribute("cid",cid);
        PageInfo<Players> pageInfo =PageInfo.of(list);
        model.addAttribute("page",pageInfo);
        return "play/index";
    }

    @RequestMapping("/del")
    @ResponseBody
    public String del(Integer pid){
        return playersService.del(pid)+"";
    }

    @PostMapping("/insert")
    public String insert(Players players, HttpServletResponse response) throws IOException {
        int i = playersService.add(players);
        if(i>0){
            response.getWriter().print("<script>alert('新增成功！');location.href='/players/list'</script>");
        }else {
            response.getWriter().print("<script>alert('新增失败！');location.href='/players/add'</script>");
        }
        return null;
    }

    @GetMapping("/get/{pid}")
    public String getList(@PathVariable Integer pid, Model model){
        model.addAttribute("players",playersService.get(pid));
        return "play/update";
    }

    @PostMapping("/update")
    public String update(Players players,HttpServletResponse response) throws IOException {
        int i = playersService.add(players);
        if(i>0){
            response.getWriter().print("<script>alert('修改成功！');location.href='/players/list'</script>");
        } else {
            response.getWriter().print("<script>alert('修改失败！');location.href='/players/list'</script>");
        }
        return null;
    }
}
